package com.jshx.sz.gdjt.activity;

import java.util.HashMap;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.bean.LoginBean;
import com.jshx.sz.gdjt.client.ClientApplication;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;
import com.jshx.sz.gdjt.thread.DataSynchronizedThread;
import com.jshx.sz.gdjt.thread.LoginDataSynchronizedThread;
import com.jshx.sz.gdjt.thread.LoginThread;
import com.jshx.sz.gdjt.util.ProgressDialogUtil;
import com.jshx.sz.gdjt.util.ToastUtil;

/**
 * 登陆界面
 * @author caxiang
 * @time 2013-08-20
 */
public class LoginActivity extends SuperActivity 
{
	/**
	 * 用户�?
	 */
	private String userName;
	
	/**
	 * 密码
	 */
	private String password;
	
	/**
	 * 账号输入�?
	 */
	private EditText userNameEdit;
	
	/**
	 * 密码输入�?
	 */
	private EditText userPassword;
	
	/**
	 * 登陆按钮
	 */
	private Button login;
	
	/**
	 * 本地数据存储对象
	 */
	private SharedPreferences sp; 
	
	private LinearLayout loginEditLayout;
	
	private CheckBox remenbePsw;
	
	private HashMap returnMap = new HashMap ();
	
	private Handler handler_syn = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
		}
	};
	

	
	/**
	 * 页面句柄
	 */
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
			ProgressDialogUtil.dismiss();
			Intent intent = new Intent(LoginActivity.this, MainActivity.class);
			LoginBean lb=new LoginBean();
			lb.setReturnMap(returnMap);
			intent.putExtra("returnMap", lb);
			ApplicationGlobal.userId=returnMap.get("userId")+"";
			LoginDataSynchronizedThread thread=new LoginDataSynchronizedThread(handler_syn);
			thread.start();
			
			startActivity(intent);
			LoginActivity.this.finish();
			 
			super.handleMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.login_activity);
		TAG = "LoginActivity";
		sp = ClientApplication.getSharePre();
		findView();
		initView();
	}

	/**
	 * 实例化页面组�?
	 */
	private void findView() 
	{
		login = (Button)findViewById(R.id.login);
		userNameEdit = (EditText)findViewById(R.id.userName);
		userPassword = (EditText)findViewById(R.id.passwd);
		loginEditLayout = (LinearLayout)findViewById(R.id.login_edit_layout);
		remenbePsw = (CheckBox)findViewById(R.id.remenbe_psw);
	}

	/**
	 * 初始化页面数据和页面组件
	 */
	private void initView() 
	{
		userNameEdit.setText(sp.getString(ApplicationGlobal.USERNAME, ""));
		userPassword.setText(sp.getString(ApplicationGlobal.PASSWORD, ""));
		remenbePsw.setChecked(true);
		loginEditLayoutUp();
		login.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				userName = userNameEdit.getText().toString();
				password = userPassword.getText().toString();
				if(userName == null || userName.length() < 1)
				{
					ToastUtil.showLongToast(LoginActivity.this, "登陆账号不能为空!");
					return;
				}
				if(password == null || password.length() < 1)
				{
					ToastUtil.showLongToast(LoginActivity.this, "登陆密码不能为空!");
					return;
				}
				ProgressDialogUtil.show(LoginActivity.this, "登录中，请稍后...", true);
				LoginThread thread = new LoginThread(handler, userName, password, returnMap)  ;
				thread.start();
			}
		});
	}
 

	/**
	 * 
	 */
	private void loginEditLayoutUp() 
	{
		Animation up = AnimationUtils.loadAnimation(this, R.anim.loginedit_up);
		up.setAnimationListener(new AnimationListener() 
		{
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				
			}
		});
		loginEditLayout.startAnimation(up);
	}
}
